package ass.dao.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import ass.beans.UserBean;
import ass.beans.UserPrivacyBean;
import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.DataAccessException;
import ass.dao.UserPrivacyDAO;

public class UserPrivacyDAOImpl implements UserPrivacyDAO {
	
	/**
	 * The service locator to retrieve database connections from
	 */
	private DBConnectionFactory services;
	
	/** Creates a new instance of UserDAOImpl */
	public UserPrivacyDAOImpl() {
		try {
			services = new DBConnectionFactory();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
		}
	}

	public UserPrivacyDAOImpl(DBConnectionFactory services) {
		this.services = services;
	}

	
	public void insert(UserPrivacyBean bean) throws DataAccessException {
		Connection con = null;
		   try { 
		 con = services.createConnection();
	     PreparedStatement stmt = con.prepareStatement(
	       "INSERT INTO PUBLIC.TBL_USER_PRIVACY " +
	       "(FIRST_NAME_SHARED, LAST_NAME_SHARED, EMAIL_SHARED, YEAR_OF_BIRTH_SHARED, FAV_GENRE_SHARED, FAV_ACTOR_SHARED, USERNAME) " +
           " VALUES ( ?, ? , ? , ? , ? , ? , ?);");
	     stmt.setBoolean(1, bean.isFirstNameShared());
	     stmt.setBoolean(2, bean.isLastNameShared());
	     stmt.setBoolean(3, bean.isEmailShared());
	     stmt.setBoolean(4, bean.isYearOfBirthShared());
	     stmt.setBoolean(5, bean.isFavGenreShared());
	     stmt.setBoolean(6, bean.isFavActorShared());
	     stmt.setString(7, bean.getUsername());
	     int n = stmt.executeUpdate();

	     stmt.close();
	     if (n != 1)
	       throw new DataAccessException("Did not insert one row into database");
	   } catch (ServiceLocatorException e) {
	       throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
	   } catch (SQLException e) {
	       throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
	   } finally {
	      if (con != null) {
	         try {
	           con.close();
	         } catch (SQLException e1) {
	           e1.printStackTrace();
	         }
	      }
	   }
	}
	
	public void update (String username, UserPrivacyBean bean) throws DataAccessException {
		
		Connection con = null;
		   try {
			    con = services.createConnection();
				String sql = "UPDATE TBL_USER_PRIVACY "+
							 "SET "+
							"	FIRST_NAME_SHARED = "+bean.isFirstNameShared() +", "+
							"	LAST_NAME_SHARED = "+bean.isLastNameShared()+", "+
							"	EMAIL_SHARED = "+bean.isEmailShared()+", "+
							"	YEAR_OF_BIRTH_SHARED = "+bean.isYearOfBirthShared()+", "+
							"	FAV_GENRE_SHARED = "+bean.isFavGenreShared()+", "+
							"	FAV_ACTOR_SHARED = "+bean.isFavActorShared()+" "+
							" WHERE username = ?";
				System.out.println(sql);
				
	            PreparedStatement stmt = con.prepareStatement(sql.toString());
	            // run the SQL statement
	            stmt.setString(1, username);
	            int result = stmt.executeUpdate();
	            if (result == 0) {
	                System.out.println("Customer not updated.");
	            } else {
	                System.out.println("Customer updated.");
	            }
	            stmt.close();
		   } catch (ServiceLocatorException e) {
				throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
		   } catch (SQLException e) {
				throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
		   } finally {
				if (con != null) {
					try {
						con.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		
	}
	
	public UserPrivacyBean getUser(String username) throws DataAccessException {

		   Connection con = null;
		   try {
		     con = services.createConnection();
		     PreparedStatement stmt = con.prepareStatement(
		       "select * from TBL_USER_PRIVACY where USERNAME = ?");
		     stmt.setString(1, username);
		     ResultSet rs = stmt.executeQuery();
				if (rs.next()) {
					System.out.println("getUSer");
					UserPrivacyBean user = createUserPrivacyBean(rs);
					return user;
				}
				stmt.close(); 
			} catch (ServiceLocatorException e) {
				throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
			} catch (SQLException e) {
				throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
			} finally {
				if (con != null) {
					try {
						con.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
			return null;
		}
	
	
	private UserPrivacyBean createUserPrivacyBean(ResultSet rs) throws SQLException {
		UserPrivacyBean userPrivacy = new UserPrivacyBean();
		userPrivacy.setUsername(rs.getString("username") );
		userPrivacy.setFirstNameShared(rs.getBoolean("FIRST_NAME_SHARED"));
		userPrivacy.setLastNameShared(rs.getBoolean("LAST_NAME_SHARED") );
		userPrivacy.setEmailShared(rs.getBoolean("EMAIL_SHARED"));
		userPrivacy.setFavGenreShared(rs.getBoolean("FAV_GENRE_SHARED"));
		userPrivacy.setYearOfBirthShared(rs.getBoolean("YEAR_OF_BIRTH_SHARED"));
		userPrivacy.setFavActorShared(rs.getBoolean("FAV_ACTOR_SHARED"));
		return userPrivacy;
	}

}
